package com.virgo.demo.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.demo.cms.converter.HelpCenterInfoConverter;
import com.virgo.demo.cms.converter.HelpCenterListConverter;
import com.virgo.demo.cms.domain.HelpCenterDo;
import com.virgo.demo.cms.domain.bo.HelpCenterBo;
import com.virgo.demo.cms.domain.vo.HelpCenterInfoVo;
import com.virgo.demo.cms.domain.vo.HelpCenterListVo;
import com.virgo.demo.cms.mapper.HelpCenterMapper;
import com.virgo.demo.cms.service.HelpCenterService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class HelpCenterServiceImpl extends ServiceImpl<HelpCenterMapper, HelpCenterDo> implements HelpCenterService {

	private final HelpCenterMapper helpCenterMapper;

	private final HelpCenterListConverter helpCenterListConverter;

    private final HelpCenterInfoConverter helpCenterInfoConverter;

	/**
	 * 根据code查找
	 * @param code String
	 * @return HelpCenterDo
	 */
	@Override
	public HelpCenterDo loadByCode(String code) {
		LambdaQueryWrapper<HelpCenterDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(HelpCenterDo::getCode,code);
		queryWrapper.last(" limit 1 ");
		return getOne(queryWrapper);
	}

	/**
	 * 编辑帮助中心
	 * @param helpCenterBo HelpCenterBo
	 */
	@Override
	public void saveHelpCenter(HelpCenterBo helpCenterBo) {
		HelpCenterDo exist = loadByCode(helpCenterBo.getCode());
		HelpCenterDo helpCenterDo = new HelpCenterDo();
		BeanUtils.copyProperties(helpCenterBo,helpCenterDo);
		if (!ObjectUtils.isEmpty(exist) && !exist.getId().equals(helpCenterDo.getId())) {
			throw new IApplicationException(helpCenterDo.getCode() + "已存在");
		}
		if (ObjectUtils.isEmpty(helpCenterDo.getId()) || helpCenterDo.getId().compareTo(0L) < 1) {
			save(helpCenterDo);
			return;
		}
		updateById(helpCenterDo);
	}

	/**
	 * 删除配置中心
	 * @param ids String
	 */
	@Override
	public void deleteHelpCenter(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
		removeByIds(idList);
	}

	/**
	 * 帮助中心列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<HelpCenterListVo>
	 */
	@Override
	public ListDataVo<HelpCenterListVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<HelpCenterDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(HelpCenterDo::getId);

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(HelpCenterDo::getName,name);
		}

		queryWrapper.select(Arrays.asList(
				 HelpCenterDo::getId
				,HelpCenterDo::getName
				,HelpCenterDo::getCode
				,HelpCenterDo::getCreatedAt
				,HelpCenterDo::getUpdatedAt
		));

		Page<HelpCenterDo> page = new Page<>(currentPage, pageSize);
		IPage<HelpCenterDo> iPage = helpCenterMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),helpCenterListConverter.convert(iPage.getRecords(), HelpCenterListVo.class));
	}

    /**
     * 详情
     * @param id Long
     * @return HelpCenterInfoVo
     */
    @Override
    public HelpCenterInfoVo findInfo(Long id) {
        return helpCenterInfoConverter.convert(getById(id),HelpCenterInfoVo.class);
    }
}